package com.quectel.cms.controller;

import com.quectel.cms.vo.util.UploadFileResp;
import com.quectel.constant.global.UploadConstants;
import com.quectel.util.common.ImageUtils;
import com.quectel.util.common.MyFileUtils;
import com.quectel.util.common.Response;
import com.quectel.util.kit.StorageKit;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

@RestController
@RequestMapping("upload")
@Tag(name = "UploadController", description = "文件上传相关api")
public class UploadController {

    @PostMapping("uploadFileWithName")
    @Operation(summary = "上传文件")
    public Response<Object> uploadFileWithName(MultipartHttpServletRequest request) throws Exception {

        Iterator<String> fileNames = request.getFileNames();
        while (fileNames.hasNext()) {
            String next = fileNames.next();
            List<MultipartFile> files = request.getFiles(next);
            for (MultipartFile file : files) {

                int i = 0;
                for (i = 0; i < UploadConstants.FILE_ACCEPT_TYPE.length; i++) {
                    if (file.getOriginalFilename().endsWith(UploadConstants.FILE_ACCEPT_TYPE[i])) {
                        break;
                    }
                }

                if (i >= UploadConstants.FILE_ACCEPT_TYPE.length) {
                    return Response.error("不支持的文件格式");
                }

                UploadFileResp resp = new UploadFileResp();
                resp.setFileName(file.getOriginalFilename());

                File tempFile = null;
                try {
                    tempFile = MyFileUtils.writeFileToTempDir(file.getInputStream(), file.getOriginalFilename(), false);
                    String url = StorageKit.uploadToPublicBucketObtainUrl(tempFile);
                    resp.setFileUrl(url);
                    return Response.ok().wrap(resp);
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (tempFile != null && tempFile.exists()) {
                        tempFile.delete();
                    }
                }
            }
        }
        return Response.error("上传失败");
    }

    @PostMapping("upFile")
    @Operation(summary = "文件上传")
    public Response<Object> upFile(MultipartHttpServletRequest request) {


        Iterator<String> fileNames = request.getFileNames();
        while (fileNames.hasNext()) {
            String next = fileNames.next();
            List<MultipartFile> files = request.getFiles(next);
            for (MultipartFile file : files) {
                int i = 0;
                for (i = 0; i < UploadConstants.FILE_ACCEPT_TYPE.length; i++) {
                    if (file.getOriginalFilename().endsWith(UploadConstants.FILE_ACCEPT_TYPE[i])) {
                        break;
                    }
                }
                if (i >= UploadConstants.FILE_ACCEPT_TYPE.length) {
                    return Response.error("不支持的文件格式");
                }

                File tempFile = null;
                try {
                    tempFile = MyFileUtils.writeFileToTempDir(file.getInputStream(), file.getOriginalFilename(), false);
                    String url = StorageKit.uploadToPublicBucketObtainUrl(tempFile);
                    return Response.ok().wrap(url);
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (tempFile != null && tempFile.exists()) {
                        tempFile.delete();
                    }
                }
            }
        }
        return Response.error("上传失败");
    }

    @PostMapping("upImgWithCompress")
    @Operation(summary = "图片上传，后端压缩")
    public Response<Object> upImgWithCompress(MultipartHttpServletRequest request) {

        Iterator<String> fileNames = request.getFileNames();
        while (fileNames.hasNext()) {
            String next = fileNames.next();
            List<MultipartFile> files = request.getFiles(next);
            for (MultipartFile file : files) {
                int i = 0;
                for (i = 0; i < UploadConstants.FILE_ACCEPT_TYPE.length; i++) {
                    if (file.getOriginalFilename().endsWith(UploadConstants.FILE_ACCEPT_TYPE[i])) {
                        break;
                    }
                }
                if (i >= UploadConstants.FILE_ACCEPT_TYPE.length) {
                    return Response.error("不支持的文件格式");
                }
                File tempFile = null;
                try {
                    tempFile = ImageUtils.compressImgWriteToTempDir(file.getInputStream(), file.getOriginalFilename(), false);
                    String url = StorageKit.uploadToPublicBucketObtainUrl(tempFile);
                    return Response.ok().wrap(url);
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (tempFile != null && tempFile.exists()) {
                        tempFile.delete();
                    }
                }
            }
        }
        return Response.error("上传失败");
    }

    @PostMapping("upImg")
    @Operation(summary = "图片上传")
    public Response<Object> upImg(MultipartHttpServletRequest request) {
        Iterator<String> fileNames = request.getFileNames();
        while (fileNames.hasNext()) {
            String next = fileNames.next();
            List<MultipartFile> files = request.getFiles(next);
            for (MultipartFile file : files) {
                int i = 0;
                for (i = 0; i < UploadConstants.FILE_ACCEPT_TYPE.length; i++) {
                    if (file.getOriginalFilename().endsWith(UploadConstants.FILE_ACCEPT_TYPE[i])) {
                        break;
                    }
                }
                if (i >= UploadConstants.FILE_ACCEPT_TYPE.length) {
                    return Response.error("不支持的文件格式");
                }

                File tempFile = null;
                try {
                    String compress = request.getParameter("compress");
                    if (StringUtils.isNotBlank(compress) && Boolean.FALSE.toString().equals(compress)) {
                        tempFile = MyFileUtils.writeFileToTempDir(file.getInputStream(), file.getOriginalFilename(), false);
                    } else {
                        //需要压缩
                        tempFile = ImageUtils.compressImgWriteToTempDir(file.getInputStream(), file.getOriginalFilename(), false);
                    }
                    String url = StorageKit.uploadToPublicBucketObtainUrl(tempFile);
                    return Response.ok().wrap(url);
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (tempFile != null && tempFile.exists()) {
                        tempFile.delete();
                    }
                }
            }
        }
        return Response.error("上传失败");
    }
}
